package bb.lanxing.lib.devices.sprint.presenter.impl;

import android.content.ComponentName;
import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Pair;
import bb.lanxing.lib.devices.api.ConnectionListener;
import bb.lanxing.lib.devices.api.SmartDevice;
import bb.lanxing.lib.devices.core.scanner.BleDeviceScanner;
import bb.lanxing.lib.devices.core.scanner.BleSmartDeviceScanner;
import bb.lanxing.lib.devices.core.scanner.DeviceScanner;
import bb.lanxing.lib.devices.core.sync.AbstractSyncPresenter;
import bb.lanxing.lib.devices.core.sync.SyncHelper;
import bb.lanxing.lib.devices.core.sync.SyncManager;
import bb.lanxing.lib.devices.core.utils.DeviceHelper;
import bb.lanxing.lib.devices.sprint.Command;
import bb.lanxing.lib.devices.sprint.SprintListener;
import bb.lanxing.lib.devices.sprint.XossDeviceController;
import bb.lanxing.lib.devices.sprint.model.SprintFirmwareModel;
import bb.lanxing.lib.devices.sprint.model.SprintHomeModel;
import bb.lanxing.lib.devices.sprint.presenter.SprintHomePresenter;
import bb.lanxing.lib.devices.sprint.utils.DeviceControllerHelper;
import bb.lanxing.lib.devices.sprint.view.SprintHomeView;
import bb.lanxing.lib.devices.utils.DeviceUtils;
import bb.lanxing.lib.devices.utils.DeviceVersionHelper;
import java.util.Map;
import rx.Subscriber;
import rx.functions.Action1;

//@Deprecated
public abstract class AbstractSprintHomePresenter extends AbstractSyncPresenter implements SprintHomePresenter, ConnectionListener, SprintListener, DeviceScanner.ScannerCallback<SmartDevice>, SyncHelper.SyncHelperCallback {
    public XossDeviceController controller;
    public String deviceAddress;
    public String deviceName;
    public SprintFirmwareModel firmwareModel;

    public String fw;
    public String hw;
    boolean isActive = false;
    boolean isConnecting;
    boolean isVisible;
    public String manufacturer;
    public String model;
    public BleDeviceScanner scanner;

    public String sn;
    public SprintHomeModel sprintModel;

    public String sw;
    public SprintHomeView view;

    protected abstract SprintFirmwareModel createSprintFirmwareModel();

    protected abstract SprintFirmwareModel createXOSSFirmwareModel(String str, String str2);

    protected String getLastAddress() {
        return null;
    }

    @Override
    public void onCmdStatus(Command command, int i, byte[] bArr) {
    }

    protected void onDownloadFirmware(Context context, String str, String str2, String str3, String str4) {
    }

    @Override
    public void onFileReceived(String str) {
    }

    @Override
    public void onStartScan() {
    }

    @Override
    public void onStopScan() {
    }

    protected void setLastAddress(String str) {
    }

    public AbstractSprintHomePresenter(SprintHomeView sprintHomeView, SprintHomeModel sprintHomeModel) {
        this.view = sprintHomeView;
        this.sprintModel = sprintHomeModel;
        initSubscriber(sprintHomeModel);
        DeviceHelper.registerConnectionStateListener(this);
        this.scanner = new BleSmartDeviceScanner(sprintHomeView.getContext().getApplicationContext(), this, DeviceHelper.getDeviceProvider());
    }

    @Override
    public Context getContext() {
        SprintHomeView sprintHomeView = this.view;
        if (sprintHomeView != null) {
            return sprintHomeView.getContext();
        }
        return null;
    }

    @Override
    protected SyncHelper createSyncHelper() {
        return new SyncHelper(new ComponentName(getContext(), getSyncService()), getAddress(), getSyncManagerName(), getArguments(), this);
    }

    @Override
    public void onAttachedToSyncManager(SyncManager syncManager) {
        SprintHomeView sprintHomeView = this.view;
        if (sprintHomeView != null) {
            sprintHomeView.onAttachedToSyncManager(syncManager);
        }
    }

    @Override
    public void onDetachedFromSyncManager(SyncManager syncManager) {
        SprintHomeView sprintHomeView = this.view;
        if (sprintHomeView != null) {
            sprintHomeView.onDetachedFromSyncManager(syncManager);
        }
    }

    private void startScan() {
        if (this.isVisible) {
            XossDeviceController xossDeviceController = this.controller;
            if (xossDeviceController == null || !xossDeviceController.isConnected()) {
                this.scanner.startScan();
            }
        }
    }

    private void stopScan() {
        this.scanner.stopScan();
    }

    @Override
    public void onStart() {
        this.isVisible = true;
        startScan();
        this.view.setNeedUpgrade(isNeedUpgrade());
    }

    @Override
    public void onPause() {
        this.isVisible = false;
        stopScan();
    }

    @Override
    public void setAddress(String str) {
        this.deviceAddress = str;
        connectDeviceIfNecessary();
        this.sprintModel.setAddress(str);
        initSprintController(str);
    }

    public void updateFirmwareInfo(String str, String str2, Uri uri) {
        if (this.view == null || this.firmwareModel == null) {
            return;
        }
        this.view.setNeedUpgrade(DeviceVersionHelper.isNeedUpdate(getFirmwareVersionWithAddress(this.deviceAddress), str));
    }

    public void initSprintController(String str) {
        if (TextUtils.isEmpty(str)) {
            return;
        }
        XossDeviceController xossDeviceController = this.controller;
        if (xossDeviceController == null || !str.equals(xossDeviceController.getAddress())) {
            XossDeviceController xossDeviceController2 = this.controller;
            if (xossDeviceController2 != null) {
                xossDeviceController2.unregisterXossDeviceListener(this);
            }
            this.controller = DeviceControllerHelper.getXossDeviceController(str);
            this.sprintModel.setAddress(str);
            XossDeviceController xossDeviceController3 = this.controller;
            if (xossDeviceController3 != null) {
                xossDeviceController3.registerXossDeviceListener(this);
                if (this.controller.isConnected()) {
                    this.sprintModel.requestBattery();
                    this.sprintModel.requestSoftwareVersion();
                    this.sprintModel.requestHardwareVersion();
                    this.sprintModel.requestManufacturer();
                    this.sprintModel.requestModel();
                    this.sprintModel.requestSerial();
                    this.sprintModel.requestMemoryRemaining();
                    this.sprintModel.requestFirmwareVersion();
                    this.sprintModel.senTime();
                }
            }
        }
    }

    private void initFirmwareSubscriber(SprintFirmwareModel sprintFirmwareModel) {
        if (sprintFirmwareModel == null) {
            return;
        }
        sprintFirmwareModel.subscribeFirmware(new Subscriber<Map<String, String>>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable th) {
            }

            @Override
            public void onNext(Map<String, String> map) {
                if (map == null || AbstractSprintHomePresenter.this.firmwareModel == null) {
                    return;
                }
                AbstractSprintHomePresenter abstractSprintHomePresenter = AbstractSprintHomePresenter.this;
                abstractSprintHomePresenter.updateFirmwareInfo(abstractSprintHomePresenter.firmwareModel.getFirmwareVersionName(), AbstractSprintHomePresenter.this.firmwareModel.getFirmwareVersionDesc(), AbstractSprintHomePresenter.this.firmwareModel.getFirmwareUri());
            }
        });
    }

    void initSubscriber(SprintHomeModel sprintHomeModel) {
        sprintHomeModel.subscribeBattery(new Action1<Integer>() {
            @Override
            public void call(Integer num) {
                if (AbstractSprintHomePresenter.this.view != null) {
                    AbstractSprintHomePresenter.this.view.setBattery(num.intValue());
                }
            }
        });
        sprintHomeModel.subscribeFirmwareVersion(new Action1<String>() {
            @Override
            public void call(String str) {
                AbstractSprintHomePresenter.this.fw = str;
                AbstractSprintHomePresenter.this.connectDeviceIfNecessary();
                if (AbstractSprintHomePresenter.this.view != null) {
                    AbstractSprintHomePresenter.this.view.setFirmware(str);
                    AbstractSprintHomePresenter.this.view.setNeedUpgrade(AbstractSprintHomePresenter.this.isNeedUpgrade());
                }
            }
        });
        sprintHomeModel.subscribeMemoryRemaining(new Action1<Pair<Long, Long>>() {
            @Override
            public void call(Pair<Long, Long> pair) {
                if (AbstractSprintHomePresenter.this.view != null) {
                    AbstractSprintHomePresenter.this.view.setMemoryInfo(((Long) pair.first).intValue(), ((Long) pair.second).intValue());
                }
            }
        });
        sprintHomeModel.subscribeManufacturer(new Action1<String>() {
            @Override
            public void call(String str) {
                AbstractSprintHomePresenter.this.manufacturer = str;
                AbstractSprintHomePresenter.this.connectDeviceIfNecessary();
                if (AbstractSprintHomePresenter.this.view != null) {
                    AbstractSprintHomePresenter.this.view.setManufacturer(str);
                }
            }
        });
        sprintHomeModel.subscribeSerial(new Action1<String>() {
            @Override
            public void call(String str) {
                AbstractSprintHomePresenter.this.sn = str;
                AbstractSprintHomePresenter.this.connectDeviceIfNecessary();
                if (AbstractSprintHomePresenter.this.view != null) {
                    AbstractSprintHomePresenter.this.view.setSerial(str);
                }
            }
        });
    }

    public void connectDeviceIfNecessary() {
        try {
            if (this.sn.isEmpty() || this.model.isEmpty() || this.manufacturer.isEmpty() || this.controller.getAddress().isEmpty() || this.isActive) {
                return;
            }
            this.sprintModel.connectDevice(this.hw, this.fw, this.sw, this.sn, this.model, this.manufacturer, this.controller.getAddress(), this.deviceName);
            this.isActive = true;
        } catch (Exception unused) {
        }
    }

    public void disconnectDeviceIfNecessary() {
        try {
            if (this.sn.isEmpty() || this.model.isEmpty() || this.manufacturer.isEmpty()) {
                return;
            }
            this.sprintModel.disconnectDevice(this.sn, this.model, this.manufacturer);
        } catch (Exception unused) {
        }
    }

    @Override
    public boolean isNeedUpgrade() {
        return this.firmwareModel != null && DeviceVersionHelper.isNeedUpdate(this.sprintModel.getFirmwareVersion(), this.firmwareModel.getFirmwareVersionName());
    }

    @Override
    public boolean isAppVersionTooLower() {
        SprintHomeModel sprintHomeModel = this.sprintModel;
        return sprintHomeModel != null && sprintHomeModel.isAppVersionTooLower();
    }

    @Override
    public boolean isFirmwareVersionTooLower() {
        SprintHomeModel sprintHomeModel = this.sprintModel;
        return sprintHomeModel != null && sprintHomeModel.isFirmwareTooLower();
    }

    @Override
    public void requestBattery() {
        SprintHomeModel sprintHomeModel = this.sprintModel;
        if (sprintHomeModel != null) {
            sprintHomeModel.requestBattery();
        }
    }

    public void requestXossGFirmwareVersion(String str, String str2) {
        if (str == null || str.isEmpty() || str2 == null || str2.isEmpty()) {
            return;
        }
        SprintFirmwareModel createXOSSFirmwareModel = createXOSSFirmwareModel(str, str2);
        this.firmwareModel = createXOSSFirmwareModel;
        initFirmwareSubscriber(createXOSSFirmwareModel);
        SprintFirmwareModel sprintFirmwareModel = this.firmwareModel;
        if (sprintFirmwareModel == null) {
            return;
        }
        sprintFirmwareModel.requestFirmwareVersion(this.view.getContext());
    }

    public void requestNewestFirmwareVersion(String str) {
        SprintFirmwareModel createXOSSFirmwareModel = DeviceControllerHelper.isInternationalVersion(str) ? createXOSSFirmwareModel(this.model, this.hw) : createSprintFirmwareModel();
        this.firmwareModel = createXOSSFirmwareModel;
        initFirmwareSubscriber(createXOSSFirmwareModel);
        SprintFirmwareModel sprintFirmwareModel = this.firmwareModel;
        if (sprintFirmwareModel == null) {
            return;
        }
        sprintFirmwareModel.requestFirmwareVersion(this.view.getContext());
    }

    @Override
    public void requestFirmwareVersion() {
        SprintHomeModel sprintHomeModel = this.sprintModel;
        if (sprintHomeModel != null) {
            sprintHomeModel.requestFirmwareVersion();
        }
    }

    @Override
    public void requestSoftwareVersion() {
        SprintHomeModel sprintHomeModel = this.sprintModel;
        if (sprintHomeModel != null) {
            sprintHomeModel.requestSoftwareVersion();
        }
    }

    @Override
    public void requestManufacturer() {
        SprintHomeModel sprintHomeModel = this.sprintModel;
        if (sprintHomeModel != null) {
            sprintHomeModel.requestManufacturer();
        }
    }

    @Override
    public void requestModel() {
        SprintHomeModel sprintHomeModel = this.sprintModel;
        if (sprintHomeModel != null) {
            sprintHomeModel.requestModel();
        }
    }

    @Override
    public void requestSerial() {
        SprintHomeModel sprintHomeModel = this.sprintModel;
        if (sprintHomeModel != null) {
            sprintHomeModel.requestSerial();
        }
    }

    @Override
    public void requestHardwareVersion() {
        SprintHomeModel sprintHomeModel = this.sprintModel;
        if (sprintHomeModel != null) {
            sprintHomeModel.requestHardwareVersion();
        }
    }

    @Override
    public void requestMemoryCapacity() {
        SprintHomeModel sprintHomeModel = this.sprintModel;
        if (sprintHomeModel != null) {
            sprintHomeModel.requestMemoryRemaining();
        }
    }

    @Override
    public void requestDeviceStatus() {
        SprintHomeModel sprintHomeModel = this.sprintModel;
        if (sprintHomeModel != null) {
            sprintHomeModel.requestDeviceStatus();
        }
    }

    @Override
    public String getFirmwareVersion() {
        SprintHomeModel sprintHomeModel = this.sprintModel;
        if (sprintHomeModel != null) {
            return sprintHomeModel.getFirmwareVersion();
        }
        return null;
    }

    @Override
    public String getFirmwareVersionWithAddress(String str) {
        SprintHomeModel sprintHomeModel = this.sprintModel;
        if (sprintHomeModel != null) {
            return sprintHomeModel.getFirmwareVersionWithAddress(str);
        }
        return null;
    }

    @Override
    public String getNewestVersionName() {
        SprintFirmwareModel sprintFirmwareModel = this.firmwareModel;
        if (sprintFirmwareModel != null) {
            return sprintFirmwareModel.getFirmwareVersionName();
        }
        return null;
    }

    @Override
    public boolean isConnected() {
        XossDeviceController xossDeviceController = this.controller;
        return xossDeviceController != null && xossDeviceController.isConnected();
    }

    @Override
    public void disconnect() {
        XossDeviceController xossDeviceController = this.controller;
        if (xossDeviceController != null) {
            xossDeviceController.disconnect();
        }
    }

    @Override
    public void connect() {
        XossDeviceController xossDeviceController = this.controller;
        if (xossDeviceController != null) {
            xossDeviceController.connect();
        }
    }

    public SprintHomeView getView() {
        return this.view;
    }

    @Override
    public void connect(SmartDevice smartDevice) {
        this.isConnecting = true;
        stopScan();
        DeviceHelper.connect(smartDevice);
    }

    @Override
    public String getAddress() {
        XossDeviceController xossDeviceController = this.controller;
        return xossDeviceController != null ? xossDeviceController.getAddress() : this.deviceAddress;
    }

    @Override
    public void destroy() {
        super.destroy();
        DeviceHelper.unregisterConnectionStateListener(this);
        XossDeviceController xossDeviceController = this.controller;
        if (xossDeviceController != null) {
            xossDeviceController.unregisterXossDeviceListener(this);
        }
        SprintHomeModel sprintHomeModel = this.sprintModel;
        if (sprintHomeModel != null) {
            sprintHomeModel.release();
        }
        BleDeviceScanner bleDeviceScanner = this.scanner;
        if (bleDeviceScanner != null) {
            bleDeviceScanner.release();
        }
        this.firmwareModel = null;
        this.sprintModel = null;
        this.controller = null;
    }

    @Override
    public void onConnectionStateChanged(SmartDevice smartDevice, int i, int i2) {
        if (smartDevice.getType() == 9 || smartDevice.getType() == 17) {
            if (i == 2) {
                this.isConnecting = false;
                this.deviceName = smartDevice.getName();
                stopScan();
                setAddress(smartDevice.getAddress());
                setLastAddress(smartDevice.getAddress());
            } else if (i == 4) {
                SprintHomeModel sprintHomeModel = this.sprintModel;
                if (sprintHomeModel != null) {
                    sprintHomeModel.clear();
                }
                this.isConnecting = false;
                startScan();
            } else if (i == 8) {
                this.deviceName = smartDevice.getName();
                setAddress(smartDevice.getAddress());
            }
            SprintHomeView sprintHomeView = this.view;
            if (sprintHomeView != null) {
                sprintHomeView.onDeviceStatus(smartDevice, i, i2);
            }
        }
    }

    @Override
    public void onProgressUpdate(Command command, float f) {
        SprintHomeView sprintHomeView = this.view;
        if (sprintHomeView != null) {
            sprintHomeView.onProgressUpdate(command, f);
        }
    }

    @Override
    public void onDevice(SmartDevice smartDevice) {
        if (!this.isVisible || smartDevice == null) {
            return;
        }
        String lastAddress = getLastAddress();
        String address = smartDevice.getAddress();
        String name = smartDevice.getName();
        String lowerCase = name != null ? name.toLowerCase() : "";
        if (lowerCase.matches("^dfu_[\\w]+")) {
            SprintHomeView sprintHomeView = this.view;
            if (sprintHomeView != null) {
                sprintHomeView.onDfuFound(address);
            }
        } else if (lastAddress == null || !lastAddress.equals(address) || !DeviceUtils.isSprintDevice(lowerCase, smartDevice.getScanRecord()) || this.isConnecting) {
        } else {
            smartDevice.setType(9);
            connect(smartDevice);
        }
    }
}
